import { DefineComponent, defineComponent, h } from 'vue'

/**
 * 设置组件自定义名称并保留原始配置
 *
 * @param {string} name - 组件自定义名称
 * @param {DefineComponent} component - 需要重命名的Vue组件
 * @throws {TypeError} 当参数不符合要求时抛出类型错误
 * @returns {DefineComponent} 包含原始配置的新组件
 */
export default function renameComponent(customName: string, asyncComponent: DefineComponent<{}, {}, any>) {
  if (typeof customName !== 'string' || customName.trim().length === 0) {
    throw new TypeError('Invalid component name provided')
  }

  if (!asyncComponent) {
    throw new TypeError('Component parameter must be a valid Vue component')
  }

  return defineComponent({
    name: customName,
    // 继承原始组件属性
    props: asyncComponent.props,
    setup(props) {
      return () => h(asyncComponent, props)
    },
  })
}
